/**
 * 
 */
package com.edu.lawdoc.api.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author xiao.hu
 *
 */
public class ZipKit {

	/**
	 * 把文件集合打成zip压缩包
	 * 
	 * @param srcFiles
	 *            压缩文件集合
	 * @param zipFile
	 *            zip文件名
	 * @throws RuntimeException
	 *             异常
	 */
	public static void toZip(List<File> srcFiles, File zipFile) throws RuntimeException {
		long start = System.currentTimeMillis();
		if (zipFile == null) {
			throw new IllegalArgumentException("压缩包文件名为空！");
		}
		if (!zipFile.getName().endsWith(".zip")) {
			throw new IllegalArgumentException("压缩包文件名异常，zipFile=" + zipFile.getPath());
		}

		ZipOutputStream zos = null;
		FileOutputStream out = null;
		FileInputStream in = null;
		try {
			out = new FileOutputStream(zipFile);
			zos = new ZipOutputStream(out);
			for (File srcFile : srcFiles) {
				byte[] buf = new byte[1024 * 10];
				zos.putNextEntry(new ZipEntry(srcFile.getName()));
				int len;

				in = new FileInputStream(srcFile);
				while ((len = in.read(buf)) != -1) {
					zos.write(buf, 0, len);
				}
				zos.setComment("我是注释");
				zos.closeEntry();
			}
			long end = System.currentTimeMillis();
			LoggerKit.debug("压缩完成，耗时：%s ms", (end - start));
			;
		} catch (Exception e) {
			LoggerKit.debug("ZipKit toZip exception, %s", e);
			throw new RuntimeException("zipFile error from ZipKit", e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					LoggerKit.debug("toZip close exception, %s", e);
					in = null;
				}
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LoggerKit.debug("toZip close exception, %s", e);
					out = null;
				}
			}
		}
	}

	public static void zipMultiFile(String filepath, String zippath, boolean dirFlag) {
		try {
			File file = new File(filepath);// 要被压缩的文件夹
			File zipFile = new File(zippath);
			ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (File fileSec : files) {
					if (dirFlag) {
						recursionZip(zipOut, fileSec, file.getName() + File.separator);
					} else {
						recursionZip(zipOut, fileSec, "");
					}
				}
			}
			zipOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void recursionZip(ZipOutputStream zipOut, File file, String baseDir) throws Exception {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File fileSec : files) {
				recursionZip(zipOut, fileSec, baseDir + file.getName() + File.separator);
			}
		} else {
			byte[] buf = new byte[1024];
			InputStream input = new FileInputStream(file);
			zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
			int len;
			while ((len = input.read(buf)) != -1) {
				zipOut.write(buf, 0, len);
			}

			input.close();
		}
	}

}
